<?php

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the PHPCodeProvider class
 */

$provider = new PHPCodeProvider();

/*
 * Expressions and statements to be used to create for statements
 */

$i = new VariableReferenceExpression('i');

$one = new PrimitiveExpression(1);
$ten = new PrimitiveExpression(10);

$empty = new SnippetExpression('');
$break = new SnippetStatement('        break');

$init4 = new AssignStatement($i, $one);
$increment = new SnippetStatement('$i++');

$echo1 = new SnippetStatement('    echo $i');

/*
 * Example 1
 */

$for1 = new ForStatement(
    $init4, 
    new BinaryOperatorExpression(BinaryOperator::LESS_THAN, $i, $ten), 
    $increment);

$for1->Statements->add($echo1);

/*
 * Example 2
 */

$for2 = new ForStatement($init4, $empty, $increment);

$for2->Statements->add(
    new IfElseStatement(
        new BinaryOperatorExpression(BinaryOperator::GREATER_THAN, $i, $ten)));

$for2->Statements[0]->TrueStatements->add($break);

$for2->Statements->add($echo1);

/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromStatement($for1, $writer, $options);
$provider->generateCodeFromStatement($for2, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';


/*
 * The output should be:
 * 
 * for ($i = 1; $i < 10; $i++) 
 * {
 *     echo $i;
 *}
 * for ($i = 1; ; $i++) 
 * {
 *    if($i > 10)
 *    {
 *        break;
 *    }
 *    echo $i;
 * }
 * 
 */
?>
